<template>
  <el-container :class=$.role_setting_panel>
    <el-header style="height:160px;">
      <el-form ref="ruleForm" label-width="120px" class="demo-ruleForm" style="margin-top:20px">
        <el-row>
          <el-col :span="7">
            <el-form-item label="HU：">
              <el-input v-model="search.exidv"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="SAP款号：">
              <el-input v-model="search.matnr"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="大SO号：">
              <el-input v-model="search.zzvbeln"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="7">
            <el-form-item label="国家：">
              <el-input v-model="search.landx"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="Packing Until：" prop="gonghao">
              <el-input v-model="search.punit"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="7">
            <el-form-item>
              <el-button type="primary" @click="query(search)">查询</el-button>
            </el-form-item>
          </el-col>
          <el-dialog title="修改" :visible.sync="dialogFormVisible">
            <el-form ref="form" class="demo-ruleForm" label-width="100px">
              <el-form-item label="SAP款号：" prop="matnr">
                {{unit.matnr}}
              </el-form-item>
              <el-form-item label="大SO号：" prop="zzvbeln">
                {{unit.zzvbeln}}
              </el-form-item>
              <el-form-item label="国家：" prop="landx">
                {{unit.landx}}
              </el-form-item>
              <el-form-item label="HU号：" prop="exidv">
                {{unit.exidv}}
              </el-form-item>
              <el-row>
                <el-col :span="12">
                  <span style="margin-left: 14%;"></span>
                  <el-form-item label="Packing Unit：" prop="cPhone">
                    <el-input v-model="unit.punit" type="text"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="   text-align: center">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="danger" @click="save()">确 定</el-button>
            </div>
          </el-dialog>
        </el-row>
      </el-form>
    </el-header>
    <el-main>
      <section class="contant_head">
        <span>图表
          <!-- <sub>Form</sub> -->
        </span>
      </section>
      <el-table :data="tableData" border style="width: 98%;margin:10px;text-align:center;" :header-cell-style="getRowClass">
        <el-table-column prop="index" label="编号" height="200px"> </el-table-column>
        <el-table-column prop="exidv" label="HU号"></el-table-column>
        <el-table-column prop="matnr" label="SAP款号"></el-table-column>
        <el-table-column prop="zzvbeln" label="大SO号"></el-table-column>
        <el-table-column prop="landx" label="国家"></el-table-column>
        <el-table-column prop="punit" label="Packing Unit"></el-table-column>
        <!-- <el-table-column prop="zip" label="创建时间"></el-table-column> -->
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10]" :page-size="limit" layout="total, sizes, prev, pager, next" :total="total" style="float:right">
        </el-pagination>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { api } from "@/fetch/api";

export default {
  name: "unit",
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      unit: {
        landx: "",
        matnr: "",
        punit: "",
        zzvbeln: "",
        exidv: ""
      },
      search: {
        matnr: "",
        zzvbeln: "",
        landx: "",
        punit: "",
        soNumber: ""
      },
      total: null,
      limit: 10,
      currentPage: 1,
      tableData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px"
    };
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:rgba(242,242,242,1)";
      } else {
        return "";
      }
    },
    save() {
      api.setPackunit(this.unit).then(response => {
        if (response.status === 100) {
          this.$message.success("修改成功");
          this.dialogFormVisible = !this.dialogFormVisible;
          this.getUser(10, this.currentPage);
        } else {
          this.$message.error("修改错误");
        }
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUser(this.limit, val);
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getUser(val, this.currentPage);
    },
    getUser(limit, pageIndex) {
      api.Hupacking(this.search, { limit: 10, pageIndex: 1 }).then(response => {
        if (response.status === 100) {
          // var result = response.result;
          // // console.log(result);
          // for (var i = 0; i < result.length; i++) {
          //   // this.tableData.push(result[i].columns[0]);
          // }
          this.tableData = response.result;
          this.tableData.forEach(function (item, index) {
            item.index = index + 1 + (pageIndex - 1) * limit;
          });
          this.total = response.result.total;
        } else {
          this.$message.error("response.message");
        }
      });
    },
    query(search) {
      api.Hupacking(this.search, { limit: 10, pageIndex: 1 }).then(response => {
        if (response.status === 100) {
          // var result = response.result;
          // for (var i = 0; i < result.length; i++) {
          //   this.tableData.push(result[i].columns[0]);
          // }
          this.tableData = response.result.list;
          this.currentPage = 1;
          this.tableData.forEach(function (item, index) {
            item.index = index + 1;
          });
          this.total = response.result.total;
        } else {
          this.$message.error(response.message);
        }
      });
    },
    handleEdit(index, row) {
      this.editNum = index;
      this.dialogFormVisible = !this.dialogFormVisible;
      this.title = "修改";
      this.unit = row;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    }
  },
  mounted() {
    this.getUser(10, this.currentPage);
  }
};
</script>

<style lang="less" scoped>
@import url('../../assets/css/index.css');
.el-header {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
  .el-input,
  .el-select,
  .el-date-picker {
    width: 100%;
  }
}
.el-main {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  padding: 0px;
  .contant_head {
    border-left: 3px solid #f7585d;
    background: #f9f9f9;
    height: 40px;
    span {
      line-height: 40px;
      font-size: 20px;
      padding-left: 10px;
      sub {
        font-size: 12px;
      }
    }
  }
}
</style>
<style>
.cell {
  text-align: center;
}

.el-input__inner {
  background: rgba(255, 255, 255, 0.08);
  padding-left: 0.2rem;
  color: #333;
}
</style>
<style lang="stylus" module="$">
.role_setting_panel {
  :global {
    .el-form--inline .el-form-item {
      margin-right: 0;
    }

    .el-input--suffix .el-input__inner {
      padding-right: 14px;
    }

    .el-input--prefix .el-input__inner {
      padding-left: 48px;
    }

    .el-dialog__header {
      background: rgba(242, 242, 242, 1);
      padding-bottom: 16px;
    }

    .el-dialog__title {
      font-weight: bold;
    }

    .el-button--primary {
      color: #fff;
      background-color: #f56c6c;
      border-color: #f56c6c;
    }
  }
}
</style>

